package com.pet.service;


import com.pet.bean.*;
import com.pet.dao.AccountDao;
import com.pet.dao.PetDao;
import com.pet.dao.PetstoreDao;
import com.pet.util.DataSourcesTool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class PetstoreService {
    PetstoreDao petstoreDao = new PetstoreDao();
    PetDao petDao = new PetDao();
    AccountDao accountDao = new AccountDao();
    //登录
    public Notification loginPetstore(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        String loginName = (String) notification.getMap().get("loginName");
        String pw = (String) notification.getMap().get("pw");
        try {
            Petstore petstore = petstoreDao.login_Petstore(con, loginName, pw);
            if (petstore != null) {
                if (petstore.getStatus() != 0) {
                    notification.setFlag(true);
                    notification.getMap().put("nowPetstore", petstore);
                    notification.getMap().put("s_id", petstore.getStore_id());
                    notification.getMap().put("name", petstore.getName());
                } else {
                    notification.setFlag(false);
                    notification.getMap().put("message", "该商店已注销，请重新操作！");
                }
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "账户或密码不正确，请重新操作！");
                notification.getMap().put("status", 1);
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //注册
    public Notification registerPetstore(Notification notification) {
        //提供数据库链接
        Connection con = DataSourcesTool.getConnection();
        //获取用户名和密码
        Petstore newPetstore = (Petstore) notification.getMap().get("newPetstore");

        try {
            //1.调用数据库的操作方法来进行登录校验
            boolean insert_Petstore = petstoreDao.openPetStore(con, newPetstore);
            //2.进行判断，作出响应
            if (insert_Petstore) {
                notification.setFlag(true);//设置标志位为true
                //将登录成功的对象保存在map中
                notification.getMap().put("newPetstore", newPetstore);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "注册失败，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        //返回结果
        return notification;
    }
    //培育宠物
    public Notification addPet(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        Pet newPet = (Pet) notification.getMap().get("newPet");
        try {
            boolean insert_Pet = petDao.createPet(con, newPet.getName(), newPet.getStore_id(), newPet.getOwner_id(), newPet.getWorth(), newPet.getType());
            //2.进行判断，作出响应
            if (insert_Pet) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("newPet", newPet);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "培育失败，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        //返回结果
        return notification;
    }
    // 查询待售宠物 --查询pet表，owner_id为null的宠物
    public Notification checkPet(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        try {
            List<Pet> pets = petDao.select_PetByPetstore(con, s_id);
            if (pets != null && pets.size() != 0) {

                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("for-sale-list", pets);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，商店没有待售宠物");
            }

        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 查询商店信息
    public Notification checkStore(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        try {
            Petstore petstore = petstoreDao.select_PetstoreByID(con, s_id);
            if (petstore != null) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("store-info", petstore);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 修改商店名字
    public Notification updateStoreName(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        String name = (String) notification.getMap().get("name");
        try {
            boolean res = petstoreDao.update_PetstoreName(con, s_id, name);
            if (res) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success", "更新成功");
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，更新失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 修改商店密码
    public Notification updateStorePw(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        String pw = (String) notification.getMap().get("pw");
        try {
            boolean res = petstoreDao.update_PetstorePw(con, s_id, pw);
            if (res) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success", "更新成功");
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，更新失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    // 修改商店财富
    public Notification updateStoreWea(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        double wea = (double) notification.getMap().get("wea");
        try {
            boolean res = petstoreDao.update_PetstoreWea(con, s_id, wea);
            if (res) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success", "更新成功");
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，更新失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //     查询交易记录
    public Notification checkAccount(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        try {
            List<Account> accounts = accountDao.selectAccountByPetstore(con, s_id);
            if (accounts != null && accounts.size() != 0) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("account-list", accounts);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，商店还没有交易记录");
            }

        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //     注销商店账号
    public Notification deleteStore(Notification notification) {
        Connection con = DataSourcesTool.getConnection();
        int s_id = (int) notification.getMap().get("s_id");
        try {
            boolean res = petstoreDao.delete_Petstore(con, s_id);
            if (res) {
                notification.setFlag(true);//设置标志位为true
                notification.getMap().put("success", "账号注销成功");
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "抱歉，账号注销失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
}
